//
//  AddAccountViewController.m
//  TuiTe
//
//  Created by 张 霄鹤 on 10-4-7.
//  Copyright 2010 xhzhang. All rights reserved.
//

#import "AddAccountViewController.h"
#import "DataManager.h"


@implementation AddAccountViewController

@synthesize txtName;
@synthesize txtPass;
@synthesize cellName;
@synthesize cellPass;
@synthesize accInfo;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(addAccount:)];
	self.navigationItem.rightBarButtonItem = bi;
	[bi release];
	
	self.navigationItem.title = @"Account";
}

/*
- (void)viewWillAppear:(BOOL)animated{
	[super viewWillAppear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	self.accInfo = nil;
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	self.txtName = nil;
	self.txtPass = nil;
	self.accInfo = nil;
}


- (IBAction)addAccount:(id)sender{
	NSString *u = self.txtName.text;
	NSString *p = self.txtPass.text;
	if(u && p){
		if([u length] > 0 && [p length] > 0){
			DataManager *dm = [DataManager sharedManager];
			if(self.accInfo && [self.accInfo.name isEqualToString:u]){
				self.accInfo.pass = p;
			}else{
				Account *acc = [[Account alloc] init];
				acc.name = u;
				acc.pass = p;
				// 添加后即可释放
				[dm addAccount:acc];
				[acc release];
			}
			[dm saveAccounts];
		}
	}
	[self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)registerTwitterAccount:(id)sender{
	UIApplication *app = [UIApplication sharedApplication];
	[app openURL:[NSURL URLWithString:@"http://twitter.com"]];
}

#pragma mark -
#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSInteger ret = 2;
    return ret;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
	
    
    // Set up the cell...
	
	NSInteger row = indexPath.row;
	NSInteger section = indexPath.section;
	if(section == 0){
		switch (row) {
			case 0:
				cell = self.cellName;
				self.txtName.placeholder = NSLocalizedString(@"ADD_ACNT_NAME",@"");
				break;
			case 1:
				cell = self.cellPass;
				self.txtPass.placeholder = NSLocalizedString(@"ADD_ACNT_PASS",@"");
				break;

			default:
				break;
		}
	}
	
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	NSString *ret = @"";
	switch (section) {
		case 0:
			ret = NSLocalizedString(@"ADD_ACNT_HEADER",@"");
			break;
		default:
			break;
	}
	return ret;
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */


/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }   
 }
 */


/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */


/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

#pragma mark -
#pragma mark MemoryManage


- (void)dealloc {
    [super dealloc];
}


@end
